import java.util.LinkedList;
import java.util.Queue;

public class MyStack {

    private Queue<Integer> qu1;
    private Queue<Integer> qu2;

    public MyStack() {
        qu1 = new LinkedList<>();
        qu2 = new LinkedList<>();
    }

    //模拟入栈
    public void push(int x) {
         if(empty()){
             qu1.offer(x);
             return;
         }
         if(!qu1.isEmpty()){//qu1不为空，qu2为空
             qu1.offer(x);
         }else{//qu2不为空，qu1为空
             qu2.offer(x);
         }
    }

    //栈顶元素出栈
    public int pop() {
        if(empty()) {
            return -1;
        }
        if(!qu1.isEmpty()) {
            int size = qu1.size();
            while(size-1 != 0) {
                qu2.offer(qu1.poll());
                size--;
            }
            return qu1.poll();
        }else {
            int size = qu2.size();
            while(size-1 != 0) {
                qu1.offer(qu2.poll());
                size--;
            }
            return qu2.poll();
        }
    }

    //返回栈顶元素
    public int top() {
        if(empty()){
            return -1;
        }
        if(! qu1.isEmpty()){
            int size = qu1.size();
            int tmp = -1;
            while(size != 0){
                tmp = qu1.poll();
                qu2.offer(tmp);//size前面的元素出队列，并进入另一个队列
                size--;
            }
            return tmp;
        }else{
            int size = qu2.size();
            int tmp = -1;
            while(size-1 != 0){
                tmp = qu2.poll();
                qu1.offer(tmp);
                size--;
            }
            return tmp;
        }
    }

    //两个队列都是空，说明模拟栈是空的
    public boolean empty() {
        return qu1.isEmpty() && qu2.isEmpty();
    }
}
